package com.carlos.gateway.security.handler;

import com.carlos.gateway.config.JwtConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.security.web.server.authentication.logout.ServerLogoutHandler;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Objects;

/**
 * @author 李健成
 * @date 2021/10/28
 */

@Component
public class JsonLogoutHandler implements ServerLogoutHandler {

    @Autowired
    private JwtConfig jwtConfig;

    @Override
    public Mono<Void> logout(WebFilterExchange webFilterExchange, Authentication authentication) {
        ServerHttpRequest request = webFilterExchange.getExchange().getRequest();
        String token = request.getHeaders().getFirst(jwtConfig.getHeader());
        if (Objects.nonNull(token)) {
            SecurityContextHolder.clearContext();
        }
        return Mono.empty();
    }

}
